import { Command } from '../constant';
import Taro from '@tarojs/taro';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import { extraPathsValue } from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async page() {
      let { user } = Taro.getStorageSync('b2b-wechat@login')
      if (user) {
        let selectDistance = Taro.getStorageSync('selectDistance');
        let params = {
          userId: user.id,
          pageNum: 1,
          status: 1,
          communityCode: selectDistance.code,
        };
        action.findeActiveList(params);
        action.getActiveKind();
      }
    },
    /**
     * 活动列表
     */
    async findeActiveList(params) {
      let { pageNum, myActiveList } = getData().main;
      action.commonChange('main.isLoading', true);
      let res = await api.pagesIndexController.findByRegisterBy(params);
      console.log('===============params=====================');
      console.log(params);
      console.log('===============params=====================');
      if (res.code == '200') {
        this.getActiveKind();
        if (pageNum == 1) {
          action.commonChange('main.myActiveList', res.data.rows);
          action.commonChange('main.totalPages', res.data.total);
        } else {
          let newList = myActiveList.concat(res.data.rows);
          action.commonChange('main.myActiveList', newList);
          action.commonChange('main.totalPages', res.data.total);
        }
      }
      action.commonChange('main.isLoading', false);
    },

    /**
     * 活动类型
     */
    async getActiveKind() {
      let res = await api.pagesIndexController.getActivityType();
      if (res.code == '200') {
        dispatch({
          type: Command.init,
          payload: {
            main: {
              activeType: res.data,
            },
          },
        });
      }
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageCMyActiveMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
